package com.fx.student.controller;

import com.fx.demo.common.resulUtils.AjaxResult;
import com.fx.student.entity.Student;
import com.fx.student.service.StudentSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collection;

@RestController
public class StudentController {
    private final org.slf4j.Logger log= org.slf4j.LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StudentSer ser;

    @Value("${server.port}")
    private int port;


    /**
     * 按照分页获取全部数据
     * @param pageNum 页数，需要获取哪一页的数据
     * @param pageSize
     * @return
     */
    @GetMapping("student")
    public AjaxResult<Collection<Student>> find(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "10") int pageSize)  {
        return ser.getStudents(pageNum,pageSize);
        /*List<Student> students=new ArrayList<>();
        Student s1=new Student("张三",18);
        Student s2=new Student("李四",28);
        Student s3=new Student("王五",38);
        students.add(s1);
        students.add(s2);
        students.add(s3);
        return ResultUtil.succ(students);*/
    }
    @GetMapping("student/{id}")
    public AjaxResult<Student> findById(@PathVariable("id") String id){
        return ser.getStudent(id);
    }
    @PostMapping("student")
    public void save(@RequestBody Student student){
        log.debug("正在保存name={},age={},createTime={},updateTime={}的对象",student.getName(),student.getAge(),student.getCreateTime(),student.getUpdateTime());
        ser.save(student);
    }
    @DeleteMapping("student/{id}")
    public void delete(@PathVariable("id") String id){
        ser.deleteById(id);
    }

    @GetMapping("hello")
    public String hello(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime localDateTime){
        log.debug("debug test");
        log.info("info test");
        log.error("error test");
        return "hello"+localDateTime.toString();
    }

    @GetMapping("getport")
    public String getPort(){
        return  "当前端口是"+this.port;
    }
}
